package com.allwees.bs.module.mms.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.allwees.bs.core.model.user.bo.MerchantDeptCreateBo;
import com.allwees.bs.core.modelbase.branch.EBranchType;
import com.allwees.bs.module.base.service.impl.BoServiceImpl;
import com.allwees.bs.module.mms.bo.MerchantCreatedBo;
import com.allwees.bs.module.mms.dao.RetailerDao;
import com.allwees.bs.module.mms.entity.Retailer;
import com.allwees.bs.module.mms.entity.RetailerApply;
import com.allwees.bs.module.mms.mq.event.RetailerCreatedEvent;
import com.allwees.bs.module.mms.req.MerchantDisableReq;
import com.allwees.bs.module.mms.req.MerchantEnableReq;
import com.allwees.bs.module.mms.req.query.RetailerQueryReq;
import com.allwees.bs.module.mms.service.RetailerApplyService;
import com.allwees.bs.module.mms.service.RetailerService;
import com.allwees.bs.module.mms.service.StoreService;
import com.allwees.core.common.constant.EStatus;
import com.allwees.core.mq.rabbitmq.MqUtil;
import com.allwees.eladmin.bs.service.BranchUserService;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.system.service.dto.UserDto;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Optional;

/**
 *
 * @author yangylsky
 * @since 2019-12-03
 */
@Slf4j
@Service
public class RetailerServiceImpl extends BoServiceImpl<RetailerDao, Retailer> implements RetailerService {
    @Autowired
    private RetailerApplyService retailerApplyService;

    @Autowired
    private BranchUserService branchUserService;

	@Autowired
	private StoreService storeService;

	@Override
	@Transactional
	public Retailer createRetailer(String retailerApplyUuid) {
        RetailerApply apply = retailerApplyService.findByUuid(retailerApplyUuid);
        if (apply == null) {
            return null;
        }

        Retailer retailer = apply.toRetailer();
        retailer.setDeptId(0L);
        save(retailer);

        //
        MerchantDeptCreateBo bo = new MerchantDeptCreateBo();
        bo.setBranchType(EBranchType.RETAILER);
        bo.setBranchUuid(retailer.getUuid());
//        bo.setDeptName(apply.getCompanyName());
        bo.setDeptName(apply.getStoreName());
        bo.setPid(1L);

        bo.setEmail(apply.getEmail());
        bo.setPhone(apply.getMobile());
        bo.setPassword(apply.getPassword());
        bo.setNickname(StringUtils.join(apply.getLastName(),apply.getFirstName()));
        UserDto user = branchUserService.createMerchantUser(bo);

        Retailer retailerUpdate = new Retailer();
        retailerUpdate.setUuid(retailer.getUuid());
        retailerUpdate.setDeptId(Optional.ofNullable(user.getDeptId()).orElse(null));
        updateByUuid(retailerUpdate);
        retailer.setDeptId(retailerUpdate.getDeptId());

		storeService.create(retailer.getUuid(), apply);

		MqUtil.send(new RetailerCreatedEvent(MerchantCreatedBo.of(retailer, apply)));

        return retailer;
	}

	@Override
	@Transactional
	public int disableRetailers(MerchantDisableReq req) {
		List<Retailer> retailers = findByUuids(req.getBranchUuids());
		int succcessNum = 0;
		for(Retailer retailer : retailers) {
			if(disable(retailer)) {
				succcessNum++;
			}
		}
		return succcessNum;
	}

	protected boolean disable(Retailer retailer) {
		Assert.notNull(retailer, "retailer must not be a null");
		Assert.isTrue(EStatus.NORMAL.equals(retailer.getEStatus()), "当前商户状态[" + retailer.getEStatus() + "]不能执行禁用");
		retailer.setEStatus(EStatus.DISABLED);
		return updateByUuid(retailer);
	}

	@Override
	@Transactional
	public int enableRetailers(MerchantEnableReq req) {
		List<Retailer> retailers = findByUuids(req.getBranchUuids());
		int succcessNum = 0;
		for(Retailer retailer : retailers) {
			if(enable(retailer)) {
				succcessNum++;
			}
		}
		return succcessNum;
	}

	@Override
	public Page<Retailer> selectPage(Page<Retailer> page, RetailerQueryReq req) {
		return baseMapper.selectPage(page,req);
	}

	protected boolean enable(Retailer retailer) {
		Assert.notNull(retailer, "retailer must not be a null");
		Assert.isTrue(EStatus.DISABLED.equals(retailer.getEStatus()), "当前商户状态[" + retailer.getEStatus() + "]不能执行启用");
		retailer.setEStatus(EStatus.NORMAL);
		return updateByUuid(retailer);
	}
}
